﻿using InParkRedemptionAPI.DataAccess;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RapidSalesAdminPortalAPI.DataAccess;
using WebAPI.Common;
using WebAPI.DataAccess;
using WebAPI.Models;
using WebAPI.Util;
using static WebAPI.Models.Enums;

namespace WebAPI.Controllers
{
    //[Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class FrIpSystemLogController : ParentController
    {
        private readonly RS_LogPortalContext _context;
        //private readonly IConfiguration _configuration;
        //private readonly ILogUtility _logUtility;
        private readonly ILogger _logger;
        //private readonly IDataProtectorUtility _dataProtectorUntility;
        public FrIpSystemLogController(RS_LogPortalContext context, 
            //IConfiguration configuration, ILogUtility logUtility,
            ILogger<FrIpSystemLogController> logger
            //, IDataProtectorUtility dataProtectorUntility
            )
        {
            _context = context;
            //_configuration = configuration;
            //_logUtility = logUtility; // sample code for DI of common utility class
            _logger = logger;
            //_dataProtectorUntility = dataProtectorUntility;
        }

        [HttpPost("GetFrIpSystemLogList")]
        public async Task<ActionResult<ResultMsg>> GetSystemLogList([FromBody] FrIpSystemLogListRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                IQueryable<FrIpSystemLog> paComont = _context.FrIpSystemLogs.AsQueryable();
                //LogTypeId     PlatformAccountLog   RedemptionPointLog   RedemptionProductLog  RedemptionAccountLog
                if (!string.IsNullOrWhiteSpace(requestModel.LogTypeId) || (requestModel.LogTypeId.Equals("AdministratorAccountLog") || requestModel.LogTypeId.Equals("SalePointLog")))
                {
                    paComont = paComont.Where(x => x.LogTypeId.Equals(requestModel.LogTypeId));
                }
                //if (!string.IsNullOrWhiteSpace(requestModel.LogTypeId) && !requestModel.LogTypeId.Equals("ActivityLog"))
                //    paComont = paComont.Where(x => x.LogTypeId.Equals("RedemptionProductLog") || x.LogTypeId.Equals("RedemptionPointLog"));

                if (!string.IsNullOrWhiteSpace(requestModel.StartDate))
                    paComont = paComont.Where(x => x.CreateDate > Convert.ToDateTime(requestModel.StartDate));
                if (!string.IsNullOrWhiteSpace(requestModel.EndDate))
                    paComont = paComont.Where(x => x.CreateDate < Convert.ToDateTime(requestModel.EndDate));
                if (!string.IsNullOrWhiteSpace(requestModel.LoginName))
                    paComont = paComont.Where(x => x.PlatformLoginName.Contains(requestModel.LoginName));
                if (!string.IsNullOrWhiteSpace(requestModel.Description))
                    paComont = paComont.Where(x => x.Description.Contains(requestModel.Description));
                if (!string.IsNullOrWhiteSpace(requestModel.FunctionModuleId))
                    paComont = paComont.Where(x => x.FunctionModuleId.Contains(requestModel.FunctionModuleId));
                if (!string.IsNullOrWhiteSpace(requestModel.SubFunctionModuleId))
                    paComont = paComont.Where(x => x.SubFunctionModuleId.Contains(requestModel.SubFunctionModuleId));
                if (!string.IsNullOrWhiteSpace(requestModel.CreateUser))
                    paComont = paComont.Where(x => x.CreateUser.ToString().Equals(requestModel.CreateUser));
                //if (!string.IsNullOrWhiteSpace(requestModel.redemptionProduct))
                //    paComont = paComont.Where(x => x.RedemptionProduct.Contains(requestModel.redemptionProduct));
                //if (!string.IsNullOrWhiteSpace(requestModel.redemptionPoint))
                //    paComont = paComont.Where(x => x.RedemptionPoint.Contains(requestModel.redemptionPoint));
                if (!string.IsNullOrWhiteSpace(requestModel.ClientHostIP))
                    paComont = paComont.Where(x => x.ClientHostIp.Contains(requestModel.ClientHostIP));
                

                var pdData = paComont.ToList().Select(x => new FrIpSystemLogListResponse()
                {
                    LogID = x.Id,
                    LoginName = x.PlatformLoginName,
                    Timestamp = x.Timestamp.HasValue ? x.Timestamp.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
                    ClientHostIP = x.ClientHostIp,
                    Description = x.Description,
                    FunctionModuleId = x.FunctionModuleId,
                    SubFunctionModuleId = x.SubFunctionModuleId,
                    //RedemptionContent = x.RedemptionContent,
                    //RedemptionProduct = x.RedemptionProduct,
                    //RedemptionProductQuantity = x.RedemptionProductQuantity.ToString(),
                    //RedemptionPoint = x.RedemptionPoint,
                }).AsQueryable();

                if (!string.IsNullOrWhiteSpace(requestModel.Field) && paComont.Count() > 0)
                    pdData = DBHelper.DataSorting(pdData, requestModel.Field, requestModel.Order);
                else
                    pdData = DBHelper.DataSorting(pdData, "Timestamp", "desc");

                var sysLogList = await BasePageOutputExtensions.ToPagedListAsync(pdData.ToList(), requestModel.Page, requestModel.PageSize);

                return resultMsg.SetResultMsg(
                     (int)ResultCode.Success,
                     HelperUtility.GetDescription(ResultCode.Success),
                     sysLogList);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                     (int)ResultCode.ErrorOnRequestUnMarshalling,
                     HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                     null);
            }
        }

        //[HttpPost("SystemLogListByLoginName")]
        //public async Task<ActionResult<ResultMsg>> SystemLogListByLoginName([FromBody] SystemLogListByLoginName requestModel)
        //{
        //    var resultMsg = new ResultMsg();
        //    try
        //    {
        //        var paComont = await _context.FrIpSystemLogs.Where(x=>x.PlatformLoginName== requestModel.LoginName && x.LogTypeId == requestModel.LogType).ToListAsync();
        //        var paData = paComont?.Select(x => new FrIpSystemLogListResponse()
        //        {
        //            LogID = x.LogId,
        //            LoginName = x.PlatformLoginName,
        //            Timestamp = x.Timestamp.Value.ToString("yyyy-MM-dd HH:mm:ss"),
        //            ClientHostIP = x.Description,
        //            Description = x.Description,
        //            FunctionModuleId = x.FunctionModuleId,
        //            SubFunctionModuleId = x.SubFunctionModuleId,

        //        })?.ToList();
        //        return resultMsg.SetResultMsg(
        //             (int)ResultCode.Success,
        //             HelperUtility.GetDescription(ResultCode.Success),
        //             paData);
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError(ex.Message);
        //        return resultMsg.SetResultMsg(
        //             (int)ResultCode.ErrorOnRequestUnMarshalling,
        //             HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
        //             null);
        //    }
        //}
    }
}
